﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using SMSSnew.DATA.CLASSES;
using SMSSnew.CLASSES;

namespace SMSSnew.UI.vicePrincipal.timetable
{
    public partial class vicePrinTtCreate : System.Web.UI.Page
    {

        String claz,grade;
        protected void Page_Load(object sender, EventArgs e)
        {

            if (!IsPostBack)
            {
                timeTableCLZ objTT = new timeTableCLZ();
                DataTable dt1;

                dt1 = objTT.loadGrade();

                if (dt1.Rows.Count != 0)
                {
                    for (int i = 0; i < dt1.Rows.Count; i++)
                    {
                        cboGrade.Items.Insert(i, dt1.Rows[i][0].ToString());
                    }
                }

            }

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            string message = createOption();
            ClientScript.RegisterStartupScript(this.GetType(), "", message, true);
        }

        protected void cboGrade_SelectedIndexChanged(object sender, EventArgs e)
        {
            DataTable dt2;
            classCLZ objClass = new classCLZ(cboGrade.SelectedItem.ToString());
            timeTableCLZ objTT = new timeTableCLZ();
            dt2 = objTT.loadClassID(objClass);

            cboClass.Items.Clear();

            if (dt2.Rows.Count != 0)
            {
                for (int i = 0; i < dt2.Rows.Count; i++)
                {
                    cboClass.Items.Insert(i, dt2.Rows[i][0].ToString());
                }
            }
            
        }

        protected void cboClass_SelectedIndexChanged(object sender, EventArgs e)
       {
           
        }

        protected string createOption()
        {
            grade = cboGrade.SelectedItem.ToString();
            claz = cboClass.SelectedItem.ToString();
            String[,] input;
            string message = "";
            
            timeTableCLZ ttCLZobj = new timeTableCLZ();

            if (ttCLZobj.checkForDuplicateClzTimetableCreation(claz) == true)
            {
                message = "alert('Class timetable is already created');";
            }
            else if (ttCLZobj.takeNumberOfSubjects(claz) != ttCLZobj.takeNumberOfAllocatedTeachers(claz))
            {
                message = "alert('Please allocate teachers for subjects in a class before creating time table.');";
            }
            else
            {
                ttCLZobj.setClassID(claz);
                ttCLZobj.setInputRecords(claz, grade);

                input = ttCLZobj.getInputRecords();
                ttCLZobj.createTimetable(input);

                message = "alert('Class Timetable is successfully created.');";

            }

            return message;
        }
    }
}
